package com.fanshuai;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ManyCost {

    private static ExecutorService executorService = Executors.newFixedThreadPool(20);

    private static void costMemory() {
        int[] arr = new int[1024];
        arr[0] = 0;

        for (int i = 0; i < 1024; i++) {
            arr[i] = i;
        }
    }

    private static void costCPU() {
        int sum = 0;
        for (int i = 0; i < 10000000; i++) {
            sum += i;
        }

        //System.out.println(sum);
    }

    public static void cost() {
        for (int i = 0; i < 10; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        costMemory();

                        try {
                            Thread.sleep(1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }

        for (int i = 10; i < 20; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        costCPU();

                        try {
                            Thread.sleep(1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
    }
}
